use CCES18_CD_OUTPUT_vv.dta, clear

* Only keep cases that are registered voters and who took post-election survey   
keep if weight_longshort_vv!=.
keep if tookpost!=1

* Adjust weights to be nationally representative 
* (based on reg voters in each strata according to 2017 catalist file)
tab OScode, gen(strata)
ebalance strata1 strata2 strata3 strata4, manual(.071 .034 .039 .37) base(weight_longshort_vv)
svyset [pw=_webal]

*Gender Recode*
recode gender 2=1 1=0, gen(female)
label define female 0 "Male" 1 "Female"
label values female female
label variable female "Female"

*Income Recode*
gen incomecat=1 if faminc<=4
replace incomecat=2 if faminc>4 & faminc<10
replace incomecat=3 if faminc<97 & faminc>=10
replace incomecat=0 if faminc==97

label define incomecat 1 "Less than $40k" 2 "$40k - $100k" 3 "Over $100k"
label values incomecat incomecat

*Ideology Recode*
gen ideo3=ideo5
recode ideo3 1/2=1 3=2 4/5=3 6=4
label define ideo3 1 "liberal" 2 "moderate" 3 "conservative"
label val ideo3 ideo3
label var ideo3 "Ideology 3 point"
recode ideo5 1=0 2=.25 3=.5 4=.75 5=1 6=.

*Party ID Recode*
recode pid3 4/5=3
recode pid7 1=0 2=.17 3=.34 4=.5 5=.67 6=.84 7=1 8=.5

*Age Recode*
gen age=2016-birthyr
gen agecat=1 if age<30
replace agecat=2 if age>29 & age<55
replace agecat=3 if age>54

label define agecat 1 "18-29" 2 "30-54" 3 "55+"
label values agecat agecat

*Race Recode*
recode race 4/9=0, gen(racecat)
replace race=3 if hispanic==1
label values racecat RACE

*Presidential approval recode*
recode  CC18_308a 2=.67 3=.33 4=0 5/9=., gen(trumpapp)
label var trumpapp "Trump approval"

*Education Recode*
recode educ 1/2=1 3/4=2 5=3 6=4, gen(educcat)
label define educcat 1 "HS or less" 2 "Some college" 3 "College degree" 4 "Postgraduate"
label values educcat educcat

recode educcat 3/4=1 1/2=0, gen(college)
label variable college "College degree"
label define college 0 "No degree" 1 "College degree"
label values college college

* Variable for South/Non-South
gen south=1 if inputstate==1 | inputstate==5 | inputstate==12 | inputstate==13 | inputstate==21 | inputstate==22 | inputstate==28 | inputstate==37 | inputstate==45 | inputstate==47 | inputstate==48 | inputstate==51 | inputstate==54 
recode south .=0

* Church attendance
recode  pew_churatd 1/2=1 3/4=2 5/7=3, gen(church)
label define attend 1 "Frequently" 2 "Infrequently" 3 "Seldom/never"
label values church attend

* DVs Recode (2018 House vote)
gen housevote=0 if CC18_412==1 & HouseCand1Party=="Democratic" & HouseCand2Party=="Republican" & CL_2018gvm!=.
replace housevote=0 if housevote==. & CC18_353==1 & HouseCand1Party=="Democratic" & HouseCand2Party=="Republican" & CL_2018gvm!=.
replace housevote=1 if CC18_412==2 & HouseCand2Party=="Republican" & HouseCand1Party=="Democratic" & CL_2018gvm!=.
replace housevote=1 if housevote==. & CC18_353==2 & HouseCand2Party=="Republican" & HouseCand1Party=="Democratic" & CL_2018gvm!=.

* DVs Recode (2016 House vote)
gen housevote16=0 if CD_300H==1 & HouseCand1Party_2016=="Democratic" & HouseCand2Party_2016=="Republican"
replace housevote16=1 if CD_300H==2 & HouseCand1Party_2016=="Democratic" & HouseCand2Party_2016=="Republican"

* Prez vote
gen prezvote=1 if CC18_317==1
replace prezvote=0 if CC18_317==2

* HH Incom change
recode CC18_302 2=.75 3=.5 4=.25 5=0, gen(hhinc)

* Build sexism scale
irt grm CC18_422c CC18_422d, intm(mc)
predict sexism, latent
gen sexism_scaled=(sexism+1.481154)/3.194589
label var sexism_scaled "Hostile sexism scale"
twoway histogram sexism_scaled, discrete title("(A)") percent aspect(1) saving(sexism, replace)

* Build racial resentment scale
irt grm CC18_422e CC18_422f CC18_422g CC18_422h
predict resentment if tookpost==2, latent
replace resentment=resentment*-1
gen resentment_scaled=(resentment+1.717928)/3.356251
label var resentment_scaled "Racial Resentment"

* Racism denial scale
irt grm CC18_422a CC18_422b, intm(mc)
predict denial, latent
replace denial=denial*-1
gen denial_scaled=(denial+ 1.15564)/3.107548
label var denial_scaled "Denial of Racism"
twoway histogram denial_scaled, discrete title("(B)") percent aspect(1) saving(denial, replace)

* Figure 1: distribution of sexism and racism
graph combine sexism.gph denial.gph, xsize(7) ysize(4) ycom iscale(1.1)
graph export figure1.pdf, replace
graph export figure1.eps, replace

* Table 2: Simple table of switching by low/high racism/sexism
gen type=1 if denial_scaled<.376 & sexism_scaled<.476
replace type=2 if denial_scaled>.376 & denial_scaled!=. & sexism_scaled<.476
replace type=3 if denial_scaled<.376 & sexism_scaled>.476 & sexism_scaled!=.
replace type=4 if denial_scaled>.376 & denial_scaled!=. & sexism_scaled>.476 & sexism_scaled!=.

gen votetype=1 if housevote==1 & housevote16==1
replace votetype=2 if housevote==0 & housevote16==1
replace votetype=3 if housevote==1 & housevote16==0
replace votetype=4 if housevote==0 & housevote16==0

svy: tab type votetype, row

* Table 1: Main analysis - Switching (modeled)
eststo clear
eststo: svy: biprobit (housevote16  = sexism_scaled denial_scaled hhinc ideo5 pid7 i.b3.church female i.agecat college  i.incomecat i.racecat  ) (housevote = sexism_scaled denial_scaled hhinc ideo5 pid7 i.b3.church female i.agecat college  i.incomecat i.racecat  )
test [housevote16]sexism_scaled=[housevote]sexism_scaled
test [housevote16]denial_scaled=[housevote]denial_scaled
test [housevote16]hhinc=[housevote]hhinc

* Figure 3
margins, at(sexism_scaled=(0(.2)1)) predict(pmarg1) saving(year1, replace) atmeans
margins, at(sexism_scaled=(0(.2)1)) predict(pmarg2) saving(year2, replace) atmeans

combomarginsplot year2 year1, legend(order(2 "2016" 1 "2018") col(1) pos(4) ring(0)) recastci(rarea) file1op(mc(none) lp(solid)) file2op(mc(none) lp(solid) lc(gs6)) aspect(1) fileci1(col(black%80) lw(none)) fileci2(col(black%40) lw(none)) ytitle("Probability of voting Republican") title("(A)") saving(sexism, replace)

margins, at(denial_scaled=(0(.2)1)) predict(pmarg1) saving(year1, replace) atmeans
margins, at(denial_scaled=(0(.2)1)) predict(pmarg2) saving(year2, replace) atmeans

combomarginsplot year2 year1, legend(order(2 "2016" 1 "2018") col(1) pos(4) ring(0)) recastci(rarea) file1op(mc(none) lp(solid)) file2op(mc(none) lp(solid) lc(gs6)) aspect(1) fileci1(col(black%80) lw(none)) fileci2(col(black%40) lw(none)) ytitle("Probability of voting Republican") title("(B)") saving(racism, replace)

graph combine sexism.gph racism.gph, ycom xcom  ysize(2.5) xsize(4.5) iscale(1)
graph export figure3.pdf, replace
graph export figure3.eps, replace

* Figure 4
margins, at(sexism_scaled=(0(.2)1)) predict(p10) saving(repdem, replace) atmeans
margins, at(sexism_scaled=(0(.2)1)) predict(p01) saving(demrep, replace) atmeans

combomarginsplot repdem demrep, title("(A)") recastci(rarea) file1op(mc(none) lp(solid)) file2op(mc(none) lp(solid) lc(gs6)) aspect(1) fileci1(col(black%80) lw(none)) fileci2(col(black%40) lw(none))  legend(order(1 "Rep -> Dem" 2 "Dem -> Rep")  col(1) pos(2) ring(0)) ytitle("Probability")   plotr(lc(none)) saving(sexism, replace)

margins, at(denial_scaled=(0(.2)1)) predict(p10) saving(repdem, replace) atmeans
margins, at(denial_scaled=(0(.2)1)) predict(p01) saving(demrep, replace) atmeans

combomarginsplot repdem demrep, title("(B)") recastci(rarea) file1op(mc(none) lp(solid)) file2op(mc(none) lp(solid) lc(gs6)) aspect(1) fileci1(col(black%80) lw(none)) fileci2(col(black%40) lw(none))  legend(order(1 "Rep -> Dem" 2 "Dem -> Rep")  col(1) pos(2) ring(0)) ytitle("Probability")    plotr(lc(none)) saving(racism, replace)

graph combine sexism.gph racism.gph, ycom xcom  ysize(2.5) xsize(4.5) iscale(1)
graph export figure4.pdf, replace
graph export figure4.eps, replace


* Figure 2: Prez vs. House 2016 
eststo clear
eststo: svy: biprobit (housevote16  = sexism_scaled denial_scaled hhinc ideo5 pid7 i.b3.church female i.agecat college  i.incomecat i.racecat  ) (prezvote = sexism_scaled denial_scaled hhinc ideo5 pid7 i.b3.church female i.agecat college  i.incomecat i.racecat  )
test [housevote16]sexism_scaled=[prezvote]sexism_scaled
test [housevote16]denial_scaled=[prezvote]denial_scaled

esttab, se  uns compress tex nogaps drop(athrho)

margins, at(sexism_scaled=(0(.2)1)) predict(pmarg1) saving(house, replace) atmeans
margins, at(sexism_scaled=(0(.2)1)) predict(pmarg2) saving(prez, replace) atmeans

combomarginsplot house prez, legend(order(2 "Vote for President" 1 "Vote for House") col(1) pos(4) ring(0)) recastci(rarea) file1op(mc(none) lp(solid)) file2op(mc(none) lp(solid) lc(gs6)) aspect(1) fileci1(col(black%80) lw(none)) fileci2(col(black%40) lw(none)) ytitle("Probability of voting Republican") title("(A)") saving(sexism, replace)

margins, at(denial_scaled=(0(.2)1)) predict(pmarg1) saving(house, replace) atmeans
margins, at(denial_scaled=(0(.2)1)) predict(pmarg2) saving(prez, replace) atmeans

combomarginsplot house prez, legend(order(2 "Vote for President" 1 "Vote for House") col(1) pos(4) ring(0)) recastci(rarea) file1op(mc(none) lp(solid)) file2op(mc(none) lp(solid) lc(gs6)) aspect(1) fileci1(col(black%80) lw(none)) fileci2(col(black%40) lw(none)) ytitle("Probability of voting Republican") title("(B)") saving(racism, replace)

graph combine sexism.gph racism.gph, ycom xcom  ysize(2.5) xsize(4.5) iscale(1)
graph export figure2.pdf, replace
graph export figure2.eps, replace



